{% extends "bills/base.html" %}
{% block js %}
{{ form.media }}
<script type="text/javascript">
var place_subjects = {
	{% for place in places %}
	{{ place.id }}: { {% for subj in place.subjects.all %}
		{{ subj.id }}: "{{ subj.name }}", {% endfor %}
	}, {% endfor %}
};

function clear_subj () {
	$('#id_subject').empty();
}

function fill_subj (place_id, subj_id) {
	subj = $('#id_subject');
	//subj.append( $('<option value="" selected="selected">---------</option>'));
	subj.append( $('<option value="">---------</option>'));
	place = place_subjects[place_id];
	for (subj_key in place) {
		subj.append( $('<option value="' + subj_key + '">' + place[subj_key] + '</option>'));
		//console.log(place[subj_id]);
	};
	$("#id_subject [value='"+subj_id+"']").attr("selected", "selected");
}

function place_select () {
	place_id = $('#id_place option:selected').val();
	//console.log(place);
	clear_subj();
	fill_subj(place_id, '');
}

$(document).ready(function() {
	$('#id_place').change(place_select);
	subj_id = $('#id_subject option:selected').val();
	//console.log(subj_id);	// '' or id
	// place_select()
	place_id = $('#id_place option:selected').val();
	console.log(place_id, ',', subj_id);	// '' or id
	clear_subj();
	//console.log(place_id);
	fill_subj(place_id, subj_id);
	// image preview
	$('.image-link').magnificPopup({
		type:'image',
		image: { verticalFit: false }
	});
});
</script>
{% include "handle_inn.html" %}
{% endblock %}
{% block body_main %}
   <form enctype="multipart/form-data" action="" method="POST">
	{% csrf_token %}
	<table border="1">
		<caption> {% if object %} Редактировать {% else %} Новый {% endif %} счет </caption>
		{{ form.as_table }}
		{% if object %}
		 <tr>
		  <td> Оригинал: </td>
		  <td> <table border="0">
		   {% for img in object.fileseq.list_items.all %}
		    <tr> <td>
		     {{ img.order }}
		     <a href="{% url 'core.views.file_get' img.pk %}" class="image-link"> <img src="{{ STATIC_URL }}img/32/png.png" title="{{ img.file.name }}"/> </a>
		     <a href="{% url 'core.views.file_get' img.pk %}"> <img src="{{ STATIC_URL }}img/32/download.png" title="Скачать"/> </a>
		    </td> </tr>
		   {% endfor %}
		  </table> </td>
		 </tr>
		{% endif %}
	</table>
	<button type="submit"> Записать </button>
	<a href="{% if object %} {% url 'bills.views.bill_view' object.pk %} {% else %} {% url 'bill_list' %} {% endif %}"> <button type="button"> Отказаться </button> </a>
   </form>
{% endblock %}
